package com.ssyx.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssyx.acl.mapper.RoleMapper;
import com.ssyx.acl.service.AdminRoleService;
import com.ssyx.acl.service.RoleService;
import com.ssyx.model.acl.AdminRole;
import com.ssyx.model.acl.Role;
import com.ssyx.vo.acl.RoleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ClassName:RoleServiceImpl
 * Description:
 *
 * @Author kay
 * @Create 2024/4/23 17:19
 * @Version 1.0
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
        implements RoleService {

    @Autowired
    private AdminRoleService adminRoleService;

    //角色条件分页查询
    @Override
    public IPage<Role> getRolePage(Page<Role> pageParam, RoleQueryVo roleQueryVo) {
        String roleName = roleQueryVo.getRoleName();
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();

        //判断条件,如果不为空,封装查询条件
        if(!StringUtils.isEmpty(roleName)){
            wrapper.like(Role::getRoleName,roleName);
        }

        //调用方法实现条件分页查询
        IPage<Role> pageModel = baseMapper.selectPage(pageParam, wrapper);
        return pageModel;
    }

    //根据用户获取角色数据
    @Override
    public Map<String, Object> getRoleByAdminId(Long adminId) {
        //查询所有的角色
        List<Role> allRolesList = baseMapper.selectList(null);

        //查询为用户已分配过的角色
        //1.根据用户id(adminId)进行查询
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId, adminId);
        List<AdminRole> adminRoleList = adminRoleService.list(wrapper);
        //2.得到角色id的集合
        List<Long> roleIdList = adminRoleList.stream()
                .map(item -> item.getRoleId())
                .collect(Collectors.toList());
        List<Role> assignRoles = new ArrayList<>();
        //遍历所有角色列表allRoleList,得到每个角色
        //判断所有角色里面是否包含已经分配角色id,封装新的List中
        for(Role role : allRolesList){
            if(roleIdList.contains(role.getId())){
                assignRoles.add(role);
            }
        }
        //建立Map,将两个集合封装到Map中
        Map<String, Object> roleMap = new HashMap<>();
        roleMap.put("allRolesList", allRolesList);
        roleMap.put("assignRoles", assignRoles);
        return roleMap;
    }

    //根据用户分配角色
    @Override
    public void saveUserRoleRealtionShip(Long adminId, Long[] roleIds) {
        //删除用户已分配的角色数据
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId,adminId);
        adminRoleService.remove(wrapper);

        //分配新的角色
        List<AdminRole> adminRoleList = new ArrayList<>();
        for(Long roleId : roleIds) {
            if(StringUtils.isEmpty(roleId)) continue;
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(roleId);
            adminRoleList.add(adminRole);
        }
        adminRoleService.saveBatch(adminRoleList);

    }

}
